﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace MyMap
{
    public partial class FieldSelectDialog : Form
    {
        private IFeatureLayer m_featureLayer;

        public FieldSelectDialog(IFeatureLayer featureLayer)
        {
            this.m_featureLayer = featureLayer;
            InitializeComponent();
            initUi();
        }

    

        private void initUi()
        {
            // 定义DataTable，以实现ComboBox的数据绑定
            DataTable tb = new DataTable();
            DataColumn col_name = new DataColumn("name", typeof(string));
            DataColumn col_alias = new DataColumn("alias", typeof(string));
            tb.Columns.Add(col_name);
            tb.Columns.Add(col_alias);

            int iCount = m_featureLayer.FeatureClass.Fields.FieldCount;
            for (int i = 0; i < iCount; i++)
            {
                IField field = m_featureLayer.FeatureClass.Fields.get_Field(i);
                DataRow row = tb.NewRow();
                row[0] = field.Name;
                row[1] = field.AliasName;
                tb.Rows.Add(row);
            }
            // 进行绑定
            comboBoxFields.DataSource = tb;
            comboBoxFields.DisplayMember = "alias"; // 控件显示的名字
            comboBoxFields.ValueMember = "name"; // 控件值的名字
        }

        public string selectFieldName()
        {
            if (comboBoxFields.SelectedItem != null)
            {
                return comboBoxFields.SelectedValue as string;
            }
            else
            {
                return null;
            }
        }

        private void Confirm_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
